#coding=utf-8
import utils
import db.scode as scode

def u2u8(s):
    return s.encode('u8')

def get_soup(code): 
    def get_url(code):
        u = 'http://www.sse.com.cn/sseportal/webapp/datapresent/SSEQueryListCmpAct?reportName=QueryListCmpRpt&COMPANY_CODE=%s'
        return u%code
    u = get_url(code)
    s = utils.get_soup(u)
    return s
    
def get_comdata_from_page(soup):
    data = []
    has_b = False
    
    #上半部分
    t1 = unicode('公司代码:', 'u8')
    table1 = soup.find(text=t1).parent.parent.parent
    c1 = table1.contents
    
    #company_code
    data.append(c1[1].contents[3].string)
    
    l = c1[3].contents[3].string.split('/')
    #acode
    a = l[0][:6]
    data.append(a)
    #bcode
    b = l[1].strip()
    if b == '-':
        data.append(None)
    else:
        has_b = True
        data.append(b[1:])
        
    #ipo, bipo
    a_list = c1[5].findAll('a')
    n = len(a_list)
    if n == 0 :
        data.append(None)
        data.append(None)
    elif n == 1:
        d1 = a_list[0].string[:10]
        data.append(d1)
        data.append(None)
    else:
        d1 = a_list[0].string[:10]
        d2 = a_list[1].string[-10]
        data.append(d1)
        data.append(d2)
    
    #bondname, bondcode
    s = c1[7].contents[3].string
    if s.startswith('-'):
        data.append(None)
        data.append(None)
    else:
        x, y = s.split(u'\uff08')
        data.append(u2u8(x)) 
        data.append(y[:-1])
        
    #name, englishname
    s = c1[9].contents[3].string
    x,y = s.split('/')
    data.append(u2u8(x))
    data.append(u2u8(y))
    
    #fullname, fullenglishname
    x,y,z = c1[11].contents[3].contents
    data.append(u2u8(x))
    data.append(u2u8(z.strip()))
    
    #register_adress
    s = c1[13].contents[3].string
    data.append( u2u8( s.strip() ))
    #contact_adress
    s = c1[15].contents[3].string
    data.append( u2u8( s.strip() ))    
    
    #下半部分
    t2 = unicode('法定代表人:', 'u8')
    table2 = soup.find(text=t2).parent.parent.parent
    c2 = table2.contents 
        
    #legal_representative, 
    s = c2[1].contents[3].string
    data.append(u2u8( s.strip()))
    #secretary
    s = c2[3].contents[3].string
    data.append(u2u8( s.strip()) )   
    #e-mail
    s = c2[5].contents[3].a.string
    data.append(s)    
    #phone
    s = c2[7].contents[3].string
    
    data.append(s)
    #website
    s = c2[9].contents[3].a.string
    data.append(s)
    
    #csrc group
    s = c2[11].contents[3].string
    #print s
    gl = [i.strip() for i in s.split('/')]
    for g in gl:
        if g == '-':
            data.append(None)
        else:
            data.append(u2u8(g))
    
    #sse group
    s = c2[13].contents[3].string
    data.append(u2u8( s.strip())  )       
    #province
    s = c2[15].contents[3].string
    data.append(u2u8( s.strip())   ) 
    
    #in180
    yes = unicode('是', 'u8')
    y = c2[19].contents[3].string
    if y == yes:
        data.append('y')
    else:
        data.append('n')
    
    #is_jwsh, jwsh_adress
    y = c2[21].contents[3].string
    if y == yes:
        data.append('y')
        a = c2[23].contents[3].string
        data.append(u2u8(a))
    else:
        data.append('n')    
        data.append(None)
        
    r = []
    for d in data:
        if d == '-':r.append(None)
        else:
            r.append(d)
    return r

def get_comdata(code):
    s = get_soup(code)
    data = get_comdata_from_page(s)    
    return data

if __name__ == '__main__':
    con = utils.get_db_connector()
    sc = scode.get_scodes(con)
    for code in sc[100:150]:
        data = get_comdata(code)
        for d in data:
            print d,'\t',
        print '\n'